/*****************webSocket start**********************/
function webSocket() {
    if (!window.webSocketObj) {
        //判断当前浏览器是否支持WebSocket
        if ('WebSocket' in window) {
            var webSocketUrl = '192.168.1.199:8080/guoyao/webSocketJoin';
            try {
                if (location.protocol === "https:") {
                    window.webSocketObj = new WebSocket("wss://" + webSocketUrl);
                } else {
                    window.webSocketObj = new WebSocket("ws://" + webSocketUrl);
                }
            } catch (e) {
                console.log(e);
            }
            //连接发生错误的回调方法
            webSocketObj.onerror = function () {
                console.log("连接失败，请重新加载！");
            };

            //连接成功建立的回调方法
            webSocketObj.onopen = function () {
                console.log("WebSocket连接成功！");
            };

            //接收到消息的回调方法
            webSocketObj.onmessage = function (event) {
                var messageObj = JSON.parse(event.data);
                dealPushMessage(messageObj);
            };
            window.timer = null;
            message = $('.message');
            messageBox = $('.message-box');
            var upkeepTip = $('.upkeep-icon').find('.tip');
            var repairTip = $('.repair-icon').find('.tip');
            function dealPushMessage (messageObj) {
                /* 地图异常点闪动 */
                if (messageObj.status) {
                    var obj = removeObjFromArrByAttr(optionMap.series[0].data, messageObj.hospitalId) || removeObjFromArrByAttr(optionMap.series[1].data, messageObj.hospitalId) || removeObjFromArrByAttr(optionMap.series[2].data, messageObj.hospitalId);
                    optionMap.series[0].data.push(obj);
                    chartMap.setOption(optionMap);
                } else {
                    var obj = removeObjFromArrByAttr(optionMap.series[0].data, messageObj.hospitalId) || removeObjFromArrByAttr(optionMap.series[1].data, messageObj.hospitalId) || removeObjFromArrByAttr(optionMap.series[2].data, messageObj.hospitalId);
                    optionMap.series[messageObj.type].data.push(obj);
                    chartMap.setOption(optionMap);
                }

                /* 列表及滚动处理 */
                try {
                    message.vTicker('stop');
                } catch (e) {
                    console.log(e);
                }
                clearTimeout(timer);
                messageBox.find('#msg-' + messageObj.keyId).remove();  // 移除相同事件
                if (!messageBox.find('#msg-' + messageObj.keyId).length) {
                    if (messageObj.type === '1') {
                        numAnimate({
                            el: repairTip,  // jquery对象
                            newNum: parseInt(repairTip.text()) + 1
                        });
                    } else {
                        numAnimate({
                            el: upkeepTip,  // jquery对象
                            newNum: parseInt(upkeepTip.text()) + 1
                        });
                    }
                }
                var icon = '<i class="' + (messageObj.type === '1' ? 'repair' : 'upkeep') + '-icon"></i>';
                messageBox.prepend('<li class="blink1" title="' + fixStr(messageObj.message) + '" id="msg-' + messageObj.keyId + '">' + icon + fixStr(messageObj.message) + '</li>');
                timer = setTimeout(function () {
                    queryRepairList();
                    queryUpkeepList();
                }, 30 * 1000);
                if (messageObj.status) {
                    setTimeout(function () {
                        messageBox.find('#msg-' + messageObj.keyId).remove();  // 移除相同事件
                    }, 30 * 1000);
                }
            };

            // 重新处理optionMap数据
            function removeObjFromArrByAttr(arr, id) {
                for (var i = 0; i < arr.length; i++) {
                    if (id === (arr[i].value ? arr[i].value[6] : '')) {
                        return arr.splice(i, 1)[0];
                    }
                }
                return false;
            }

            //连接关闭的回调方法
            webSocketObj.onclose = function () {
                console.log("WebSocket已关闭！");
            };

            //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
            window.onbeforeunload = function () {
                closeWebSocket();
            };

            //关闭WebSocket连接
            function closeWebSocket() {
                webSocketObj.close();
            }
        } else {
            alert('当前浏览器不支持 websocket！');
        }
    }
}
/*****************webSocket end**********************/